#define MDTX_USE_RCPPARMADILLO

#include <RcppArmadillo.h>
using namespace Rcpp;

#include "mdtx-online/indicator/indicator_type_misc.h"
using namespace mdtx::online;

class pmomentum_c
{
private:
    indicator::pmomentum pm;

public:
    explicit pmomentum_c(int period)
        : pm(period)
    {
    }

    List run(NumericVector x)
    {
        R_xlen_t n = x.length();
        NumericVector Rlog(n);
        NumericVector Rcum(n);
        NumericVector Rmean(n);

        pm(x.begin(), x.end(), Rlog.begin(), Rcum.begin(), Rmean.begin());

        return List::create(
            Named("Rlog") = Rlog,
            Named("Rcum") = Rcum,
            Named("Rmean") = Rmean
        );
    }
};

//' Price momentum
//'
//' @param p Numeric vector Price
//' @param period Period of rolling window
//' @return List of 1-period log return, cumulative log return, mean log return
//' @export
// [[Rcpp::export]]
List pmomentum(NumericVector p, int period)
{
    pmomentum_c c(period);
    return c.run(p);
}

class pmomentum_ew_c
{
private:
    indicator::pmomentum_ew pm;

public:
    explicit pmomentum_ew_c(int period)
        : pm(period)
    {
    }

    List run(NumericVector x)
    {
        R_xlen_t n = x.length();
        NumericVector Rlog(n);
        NumericVector Rcum(n);
        NumericVector Rmean(n);

        pm(x.begin(), x.end(), Rlog.begin(), Rcum.begin(), Rmean.begin());

        return List::create(
            Named("Rlog") = Rlog,
            Named("Rcum") = Rcum,
            Named("Rmean") = Rmean
        );
    }
};

//' Price momentum, exponential weight
//'
//' @param p Numeric vector Price
//' @param period Period of rolling window
//' @return List of 1-period log return, cumulative log return, mean log return
//' @export
// [[Rcpp::export]]
List pmomentum_ew(NumericVector p, int period)
{
    pmomentum_ew_c c(period);
    return c.run(p);
}
